package zozopo.warehouse;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import zozopo.background.operator.operations.OperatorTB;
import zozopo.background.operator.operations.OperatorlimitedTB;
import zozopo.flow.comm.FlowCode;
import zozopo.flow.operations.FlowOS;
import zozopo.flow.operations.FlowTB;
import zozopo.flow.operations.RecordlistOS;
import zozopo.flow.operations.RecordlistTB;

import zozopo.warehouse.operations.PurchaseOS;
import zozopo.warehouse.operations.PurchaseTB;
import zozopo.warehouse.operations.PurchaselistOS;
import zozopo.warehouse.operations.PurchaselistTB;


import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class ManagePurchaselistAction extends ActionSupport implements Action,SessionAware{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static final String limitedCode ="WAREHOUSE.PURCHASELIST";
	
	//传入参数
	private String searchStr = "";
	private String flowCode = "";
	//分页参数
	private int beginPageNum = 0;
	private int pageSize = 20;
	private int rowNum = 0;
	
	//传出参数
	private List<PurchaselistTB> purchaselistList;
	private Map<Integer,RecordlistTB> flowRecordlistMap;
	
	private Map<Integer,List<PurchaseTB>> purchaseListMap;
	
	Map<String,FlowTB> flowMap;
	
	//各种提示
	private int dayCount;
	private double dayTotal;
	
	private int flowWarehouseStartNum;
	private int flowWarehouseConfirmNum;
	private int flowWarehouseCancelNum;
	private int flowWarehouseOrderNum;
	private double flowWarehouseOrderPrice;
	private int flowWarehousePayNum;
	
	private Map<Object, Object> session;
	public void setSession(Map arg0) {
		// TODO Auto-generated method stub
		session = arg0;
	}
	
	public String execute(){
		
		

		PurchaselistOS purchaselistOS = new PurchaselistOS();
		PurchaseOS purchaseOS = new PurchaseOS(purchaselistOS.getConn());
		FlowOS flowOS = new FlowOS(purchaselistOS.getConn());
		RecordlistOS recordlistOS = new RecordlistOS(purchaselistOS.getConn());
		
		if(!flowCode.equals("")){
			purchaselistList = purchaselistOS.selectByFlowCode(flowCode,FlowCode.WAREHOUSE_PURCHASELIST,beginPageNum, pageSize);
			rowNum = purchaselistOS.getRowNumForFlowCode(flowCode,FlowCode.WAREHOUSE_PURCHASELIST); 
		}else if(searchStr.equals("")){
			purchaselistList = purchaselistOS.select(beginPageNum, pageSize);
			rowNum = purchaselistOS.getRowNum(); 
			
		}else{
			//receiptlistList = receiptlistOS.selectBySearchStr(searchStr, beginPageNum, pageSize);
			purchaselistList = purchaselistOS.selectBySearchStr(searchStr, beginPageNum, pageSize);
			rowNum = purchaselistOS.getRowNumForSearchStr(searchStr);
		}
		
		String inStr = "(";
		boolean isFirst = true;
		
		if(purchaselistList == null){
			return null;
		}
		
		for(PurchaselistTB Purchaselist:purchaselistList){
			if(isFirst){
				inStr += Purchaselist.getId_purchaselist();
				isFirst = false;
			}else{
				inStr += "," + Purchaselist.getId_purchaselist();
			}
		}
		inStr +=")";

		
		purchaseListMap = purchaseOS.getListMap(inStr);
		
		flowRecordlistMap = recordlistOS.getRecordlistMapByKindForPurchaselistList(purchaselistList);
		
		//各种提示
		dayCount = purchaselistOS.getTodayCountNum();
		dayTotal = purchaselistOS.getTodayTotal();
		//long currentTimeMillis = System.currentTimeMillis();
		
		flowMap = flowOS.getFlowCodeMapByKind("FLOW.WAREHOUSE.PURCHASELIST");
		flowWarehouseStartNum = flowMap.get("FLOW.WAREHOUSE.PURCHASELIST.START").getNumber_flow();
		flowWarehouseConfirmNum = flowMap.get("FLOW.WAREHOUSE.PURCHASELIST.CONFIRM").getNumber_flow();
		flowWarehouseCancelNum = flowMap.get("FLOW.WAREHOUSE.PURCHASELIST.CANCEL").getNumber_flow();
		flowWarehouseOrderNum = flowMap.get("FLOW.WAREHOUSE.PURCHASELIST.ORDER").getNumber_flow();
		flowWarehousePayNum = flowMap.get("FLOW.WAREHOUSE.PURCHASELIST.PAY").getNumber_flow();
		
		flowWarehouseOrderPrice = recordlistOS.sumPurchaselistByFlowCode("FLOW.WAREHOUSE.PURCHASELIST.ORDER");
		//*/
		/*
		flowWarehouseStartNum = flowOS.getFlow("FLOW.WAREHOUSE.PURCHASELIST.START").getNumber_flow();
		flowWarehouseConfirmNum = flowOS.getFlow("FLOW.WAREHOUSE.PURCHASELIST.CONFIRM").getNumber_flow();
		flowWarehouseCancelNum = flowOS.getFlow("FLOW.WAREHOUSE.PURCHASELIST.CANCEL").getNumber_flow();
		flowWarehouseOrderNum = flowOS.getFlow("FLOW.WAREHOUSE.PURCHASELIST.ORDER").getNumber_flow();
		flowWarehousePayNum = flowOS.getFlow("FLOW.WAREHOUSE.PURCHASELIST.PAY").getNumber_flow();
		
		//*/
		//long currentTimeMillis2 = System.currentTimeMillis();
		
	//	System.out.println(currentTimeMillis2-currentTimeMillis);
		
		
		
		purchaselistOS.releaseConnection();
		return SUCCESS;
		
	}
	
	public void validate() {
		//权限处理
		OperatorTB operator = (OperatorTB)session.get("operatorInfo"); 
	
		if(operator == null){
			addFieldError("errormessage",getText("operator.timeoutornotlogin"));
			return ; 
		} 
		
		//用户权限
		Map<String,OperatorlimitedTB> operatorlimitedMap = (Map<String,OperatorlimitedTB>)session.get("operatorlimitedMap");
		
		if(operatorlimitedMap.get(limitedCode) == null){
			addFieldError("errormessage","用户没有查看进货权限.");
			return;
		}
	}
	
	public String getSearchStr() {
		return searchStr;
	}
	public void setSearchStr(String searchStr) throws UnsupportedEncodingException {
		this.searchStr = java.net.URLDecoder.decode(searchStr, "UTF-8");
		this.searchStr = this.searchStr.trim();
	}
	public int getBeginPageNum() {
		return beginPageNum;
	}
	public void setBeginPageNum(int beginPageNum) {
		this.beginPageNum = beginPageNum;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getRowNum() {
		return rowNum;
	}
	public void setRowNum(int rowNum) {
		this.rowNum = rowNum;
	}

	public List<PurchaselistTB> getPurchaselistList() {
		return purchaselistList;
	}

	public void setPurchaselistList(List<PurchaselistTB> purchaselistList) {
		this.purchaselistList = purchaselistList;
	}

	public Map<Integer, List<PurchaseTB>> getPurchaseListMap() {
		return purchaseListMap;
	}

	public void setPurchaseListMap(Map<Integer, List<PurchaseTB>> purchaseListMap) {
		this.purchaseListMap = purchaseListMap;
	}

	public static String getLimitedcode() {
		return limitedCode;
	}

	public Map<Integer, RecordlistTB> getFlowRecordlistMap() {
		return flowRecordlistMap;
	}

	public void setFlowRecordlistMap(Map<Integer, RecordlistTB> flowRecordlistMap) {
		this.flowRecordlistMap = flowRecordlistMap;
	}

	public int getDayCount() {
		return dayCount;
	}

	public void setDayCount(int dayCount) {
		this.dayCount = dayCount;
	}

	public double getDayTotal() {
		return dayTotal;
	}

	public void setDayTotal(double dayTotal) {
		this.dayTotal = dayTotal;
	}

	public int getFlowWarehouseStartNum() {
		return flowWarehouseStartNum;
	}

	public void setFlowWarehouseStartNum(int flowWarehouseStartNum) {
		this.flowWarehouseStartNum = flowWarehouseStartNum;
	}

	public int getFlowWarehouseConfirmNum() {
		return flowWarehouseConfirmNum;
	}

	public void setFlowWarehouseConfirmNum(int flowWarehouseConfirmNum) {
		this.flowWarehouseConfirmNum = flowWarehouseConfirmNum;
	}

	public int getFlowWarehouseCancelNum() {
		return flowWarehouseCancelNum;
	}

	public void setFlowWarehouseCancelNum(int flowWarehouseCancelNum) {
		this.flowWarehouseCancelNum = flowWarehouseCancelNum;
	}

	public int getFlowWarehouseOrderNum() {
		return flowWarehouseOrderNum;
	}

	public void setFlowWarehouseOrderNum(int flowWarehouseOrderNum) {
		this.flowWarehouseOrderNum = flowWarehouseOrderNum;
	}

	public String getFlowCode() {
		return flowCode;
	}

	public void setFlowCode(String flowCode) {
		this.flowCode = flowCode;
	}

	public int getFlowWarehousePayNum() {
		return flowWarehousePayNum;
	}

	public void setFlowWarehousePayNum(int flowWarehousePayNum) {
		this.flowWarehousePayNum = flowWarehousePayNum;
	}

	public double getFlowWarehouseOrderPrice() {
		return flowWarehouseOrderPrice;
	}

	public void setFlowWarehouseOrderPrice(double flowWarehouseOrderPrice) {
		this.flowWarehouseOrderPrice = flowWarehouseOrderPrice;
	}
	

}
